﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace TechMadeira.Data
{
    public class DependableCollection<S> : ICollection<S>
    {
        public DependableCollection( IList aList )
        {
            Dependable = aList;
        }

        public IList Dependable { get; private set; } 

        public void Add( S item )
        {
            Dependable.Add( item );
        }

        public void Clear()
        {
            throw new NotSupportedException();
        }

        public bool Contains( S item )
        {
            return Dependable.Contains( item );
        }

        public void CopyTo( S[] array, int arrayIndex )
        {
            Dependable.CopyTo( array, arrayIndex );
        }

        public int Count
        {
            get { return Dependable.OfType<S>().Count(); }
        }

        public bool IsReadOnly
        {
            get { return Dependable.IsReadOnly; }
        }

        public bool Remove( S item )
        {
            if ( ! Dependable.Contains( item ) ) return false;
            Dependable.Remove( item );
            return true;
        }

        public IEnumerator<S> GetEnumerator()
        {
            IEnumerator<S> enumerator = Dependable.OfType<S>().GetEnumerator();
            return enumerator;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Dependable.OfType<S>().GetEnumerator();
        }
    }
}
